package cn.atzxc.qy.controller;

import cn.atzxc.qy.mapper.AddressMapper;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLOutput;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@RestController
public class TestController {
    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/test")
    public String test() {
        return "test";
    }

    @RequestMapping("/test01")
    public String test01(@RequestHeader("token") String token, @RequestParam("addressId") long addressId) {
        System.out.println("开始测试");
        int i = addressMapper.updateIsDefaultByAddress(addressId, 1);
        return "test01";
    }


    // 生成订单编号的方法
    public static String generateOrderNumber(long userId) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateTime = now.format(formatter);
        System.out.println(dateTime);
        // 获取当前时间戳
        long timestamp = Instant.now().toEpochMilli();
        // 将用户ID和时间戳拼接起来作为订单编号
        String orderNumber = userId +""+ timestamp;
        return orderNumber;
    }

    // 测试
       public static void main(String[] args) {
        // 假设用户ID为12345
        long userId = 12345;
        // 生成订单编号
        String orderNumber = generateOrderNumber(userId);
        // 打印生成的订单编号
        System.out.println("订单编号：" + orderNumber);
    }











    //    OSS测试
    public static void OssUtils(String[] args) throws IOException {
        //==========================下面五项配置修改成自己的========================//
        //区域
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
//    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
        //访问id
        String accessKeyId = "LTAI5tHkm2f4tLAUghiQkpiy";
        //访问秘钥
        String accessKeySecret = "5T88u1RnhzSUcxQIBcQCm1frIfNHUC";
        //桶名称
        String bucketName = "atzxc-blog";
        //访问URL
        String url_base = "https://atzxc-blog.oss-cn-hangzhou.aliyuncs.com";


        // 要上传的文件URL
        String fileUrl = "https://example.com/file_to_upload.txt";

        // 指定上传后的路径
        String uploadPath = url_base + "/qp/";

        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 从URL中获取文件流
            URL url = new URL(fileUrl);
            InputStream inputStream = url.openStream();

            // 指定上传的对象Key（路径）
            String objectKey = uploadPath + "uploaded_file.txt";

            // 上传文件到 OSS
            ossClient.putObject(bucketName, objectKey, inputStream);

            System.out.println("文件上传成功");

            // 构建上传后的URL信息
            String uploadedUrl = "https://" + bucketName + "." + endpoint + "/" + objectKey;
            System.out.println("上传后的URL：" + uploadedUrl);
        } finally {
            // 关闭 OSS 客户端
            ossClient.shutdown();
        }
    }
























}
